﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;


namespace OfficeProcessing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        void mergeDocumentsTest(List<string> documentFiles)
        {

            _Application oWord = new Microsoft.Office.Interop.Word.Application();            
            _Document oDoc = oWord.Documents.Add();
            Selection oSelection = oWord.Selection;
            
            foreach (string documentFile in documentFiles)
            {
                _Document oCurrentDocument = oWord.Documents.Add(documentFile);
                copyPageSetup(oCurrentDocument.PageSetup, oDoc.Sections.Last.PageSetup);
                oCurrentDocument.Range().Copy();
                oSelection.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting);
                if (!Object.ReferenceEquals(documentFile, documentFiles.Last()))
                    oSelection.InsertBreak(WdBreakType.wdSectionBreakNextPage);
            }

            oDoc.SaveAs("testdoc.doc");
            oDoc.Close();

            //TODO: release objects, close word application

        }

        void copyPageSetup(PageSetup source, PageSetup target)
        {
            target.PaperSize = source.PaperSize;

            //target.Orientation = source.Orientation; //not working in word 2003, so here is another way
            if (!source.Orientation.Equals(target.Orientation))
                target.TogglePortrait();

            target.TopMargin = source.TopMargin;
            target.BottomMargin = source.BottomMargin;
            target.RightMargin = source.RightMargin;
            target.LeftMargin = source.LeftMargin;
            if( source.FooterDistance >= 0 && source.FooterDistance <=1584)
            {
                target.FooterDistance = source.FooterDistance;
            }
            if (source.HeaderDistance >= 0 && source.HeaderDistance <= 1584)
            {
                target.HeaderDistance = source.HeaderDistance;
            }            
            target.LayoutMode = source.LayoutMode;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            var app = new Microsoft.Office.Interop.Word.Application();

            //Show the Word instance.
            //If the code runs too slowly, you can show the application at the end of the program
            //Make sure it works properly first; otherwise, you'll get an error in a hidden window
            //(If it still runs too slowly, there are a few other ways to reduce screen updating)
            app.Visible = true;

            //We need a reference to the source document
            //It should be possible to get a reference to an open Word document, but I haven't tried it
            var doc = app.Documents.Open(@"D:\PAS\2012 PAS\HVN-HR-GD Compose Project Performance Checkpoint Form.docx");
            //(Can also use .docx)

            int pageCount = doc.Range().Information[WdInformation.wdNumberOfPagesInDocument];

            //We'll hold the start position of each page here
            int pageStart = 0;
           
            for (int currentPageIndex = 1; currentPageIndex <= pageCount; currentPageIndex++)
            {
                //This Range object will contain each page.
                var page = doc.Range(pageStart);

                //Generally, the end of the current page is 1 character before the start of the next.
                //However, we need to handle the last page -- since there is no next page, the 
                //GoTo method will move to the *start* of the last page.
                if (currentPageIndex < pageCount)
                {
                    //page.GoTo returns a new Range object, leaving the page object unaffected
                    page.End = page.GoTo(
                        What: WdGoToItem.wdGoToPage,
                        Which: WdGoToDirection.wdGoToAbsolute,
                        Count: currentPageIndex + 1
                    ).Start - 1;
                }
                else
                {
                    page.End = doc.Range().End;
                }
                pageStart = page.End + 1;

                //Copy and paste the contents of the Range into a new document
                //page.Copy();
                //var doc2 = app.Documents.Add();
                //doc2.Range().Paste();
                var doc2 = app.Documents.Add();                
                
                Selection oSelection = app.Selection;
                copyPageSetup(doc.PageSetup, doc2.PageSetup);
                page.Copy();
                doc2.Range().PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting);
                    //if (!Object.ReferenceEquals(documentFile, documentFiles.Last()))
                    //    oSelection.InsertBreak(WdBreakType.wdSectionBreakNextPage);


                doc.Activate();
                page.Select();
                doc.Application.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
                //doc.Application.ActiveDocument.ActiveWindow.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Select();
                doc.Application.ActiveDocument.ActiveWindow.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Select();
                doc.Application.ActiveDocument.ActiveWindow.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Copy();
                doc2.Activate();
                //doc2.Range(1, 1).Select();
                doc2.Application.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
                doc2.Application.ActiveDocument.ActiveWindow.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Select();
                doc2.Application.ActiveDocument.ActiveWindow.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting);
            }
        }
    }
}
